﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp55.Models
{
    public class User : INotifyPropertyChanged, INotifyDataErrorInfo
    {
        private Dictionary<string, List<string>> errors = new();
        private string userName = null!;
        public string UserName
        {
            get { return userName; }
            set
            {
                bool flag = false;  //是否出现过错误信息
                userName = value;

                if (string.IsNullOrEmpty(userName))
                {
                    /*throw new Exception("Error : UserName is Null!");*/   //这种方式是不友好的，
                    string info = "Error : UserName is Null!";
                    SetErrors(nameof(UserName), new List<string>() { info });

                    flag = true;
                }

                if (userName.Length < 3)
                {
                    string info = "Error : UserName Length <3 !";
                    SetErrors(nameof(UserName), new List<string>() { info });

                    flag = true;
                }

                if (userName.Length > 10)
                {
                    string info = "Error : UserName Length >10 !";
                    SetErrors(nameof(UserName), new List<string>() { info });

                    flag = true;
                }

                //如果没有错误，则清除
                if (!flag)
                {
                    ClearErrors(nameof(UserName));
                }

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UserName)));
            }
        }

        private string userPwd = null!;
        public string UserPwd
        {
            get => userPwd;
            set
            {
                userPwd = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(userPwd)));
            }
        }

        private void SetErrors(string propertyName, List<string> err)
        {
            if (errors.ContainsKey(propertyName))
            {
                errors.Remove(propertyName);
            }
            errors.Add(propertyName, err);
            ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(UserName)));
        }

        private void ClearErrors(string propertyName)
        {
            if (errors.ContainsKey(propertyName))
            {
                errors.Remove(propertyName);
            }
        }
        public event PropertyChangedEventHandler? PropertyChanged;

        #region INotifyDataErrorInfo
        public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
        public IEnumerable GetErrors(string? propertyName)
        {
            if (!string.IsNullOrEmpty(propertyName))
            {
                if (errors.Keys.Contains(propertyName))
                {
                    return errors[propertyName];
                }
                else
                {
                    return null!;
                }
            }
            return null!;
        }

        public bool HasErrors => errors.Count > 0;


        #endregion
    }
}
